package com.shinow.capacitor.plusins;

import android.speech.tts.TextToSpeech;

import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

@CapacitorPlugin(name = "TTS")
public class TTSPlugin extends Plugin {

    private TTS implementation = new TTS();

    @PluginMethod
    public void init(PluginCall call) {
        TextToSpeech tts = new TextToSpeech(getContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status != TextToSpeech.SUCCESS) {
                    implementation.clearInstance();
                    call.reject(implementation.EngineLoadFailed);
                } else {
                    call.resolve();
                }
            }
        }, call.getString("Engine"));

        implementation.setInstance(tts);
    }

    @PluginMethod
    public void speak(PluginCall call) {
        implementation.speak(call);
    }

}
